package com.cat.greedyAlgorithm;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/decrease-elements-to-make-array-zigzag/description/
 * @create 2025/8/25 09:33
 * @since JDK17
 */

public class Solution27 {
    public int movesToMakeZigzag(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int ans1 = 0, n = nums.length, ans2 = 0;
        for (int i = 1; i < n; i += 2) {
            ans1 += Math.max(0, nums[i] - Math.min(nums[i - 1], nums[i == n - 1 ? i - 1 : i + 1]) + 1);

        }
        for (int i = 0; i < n; i += 2) {
            ans2 += Math.max(0, nums[i] - Math.min(nums[i == 0 ? i + 1 : i - 1], nums[i == n - 1 ? i - 1 : i + 1]) + 1);
        }
        return Math.min(ans1, ans2);
    }
}
